/*
	微信的api封装
*/

export default{
	videoAd:null,//激励视频对象
	/*
		@request发起 HTTPS 网络请求
		@param
			url:接口地址
			data:请求接口数据
			header:请求头
			method:HTTP 请求方法
			success(res):接口调用成功返回
			fail(res):接口调用失败返回

	*/
	request(param){
		wx.request({
			url:param.url,
			data:param.data,
			header:param.header?param.header:{"content-type":"application/json"},
			method:param.method?param.method:"GET",
			success(res){
				param.success&&param.success(res);
			},
			fail(res){
				param.fail&&param.fail(res);
			}
		})
	},

	/*
		@showShareMenu打开小程序右上角分享
	*/
	showShareMenu(){
		wx.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		})
	},

	/*
		@onShareAppMessage监听点击右上角发送给好友
	*/
	onShareAppMessage(param){
		wx.onShareAppMessage(function(){
		    return {
			    title:param.title?param.title:"",
				imageUrl:param.imageUrl?param.imageUrl:"",
				query:param.query?param.query:"",
				imageUrlId:param.imageUrlId?param.imageUrlId:"",
				promise:param.promise?param.promise:"",
		    }
		})
	},

	/*
		@onShareTimeline监听点击右上角发送到朋友圈
	*/
	onShareTimeline(param){
		wx.onShareTimeline(function(){
		    return {
			    title:param.title?param.title:"",
				imageUrl:param.imageUrl?param.imageUrl:"",
				query:param.query?param.query:"",
		    }
		})
	},

	/*
		@shareAppMessage小游戏点击按钮分享
	*/
	shareAppMessage(param){
		console.log("param",param)
		param=param?param:{};
		wx.shareAppMessage({
			title:param.title?param.title:"",
			imageUrl:param.imageUrl?param.imageUrl:"",
			query:param.query?param.query:"",
			imageUrlId:param.imageUrlId?param.imageUrlId:"",
		})
	},

	/*
		@showToast弹框
	*/
	showToast(param){
		wx.showToast(param)
	},

	/*
		@chooseImage选择相册
	*/
	chooseImage(param){
		wx.chooseImage(param)
	},
	/*
		@uploadFile上传文件
	*/
	uploadFile(param){
		wx.uploadFile(param)
	},
	/*
		@showLoading加载中
	*/
	showLoading(param){
		wx.showLoading(param)
	},
	/*
		@hideLoading加载中
	*/
	hideLoading(param){
		wx.hideLoading(param)
	},
	/*
		@navigateToMiniProgram跳转小程序
	*/
	navigateToMiniProgram(param){
		wx.navigateToMiniProgram(param)
	},
	/*
		@createRewardedVideoAd创建激励视频

	*/
	createRewardedVideoAd(adUnitId,success,fail){
		if (wx.createRewardedVideoAd) {
		    // 加载激励视频广告
		    this.videoAd = wx.createRewardedVideoAd({
		      adUnitId: adUnitId
		    })
			this.videoAd.onLoad(() => {
			    console.log('激励视频加载成功') 
			    
			})
		    //捕捉错误
		    this.videoAd.onError(err => {
				if(fail){fail("fail")}
		    })
		    // 监听关闭
		    this.videoAd.onClose((status) => {
				if(success){success(status)}
		    })
		}else{
			if(fail){fail("fail")}
		}
	},

	/*
		@showVideoAd打开激励视频
	*/
	showVideoAd(){
		this.videoAd.show().catch(err => {
			console.log("err",err)
			if(err){
				if(fn){fn(err)}
			}
			  // 失败重试
			  this.videoAd.load().then(() => this.videoAd.show())
		})
	}




}